from appium.webdriver.common.appiumby import AppiumBy

from base.base_page import BasePage

class HomePage(BasePage):
    # 搜索框
    SEARCH_BOX = (AppiumBy.XPATH, "//android.widget.EditText[@hint='查询运单信息/应用信息']")
    # 搜索框左侧图标（可选，若需精确定位可补充  等）
    SEARCH_ICON = (
    AppiumBy.XPATH, "//android.widget.ImageView[@='...' and @content-desc='搜索图标']")   
    # 右上角 - 扫码图标
    SCAN_CODE_ICON = (
    AppiumBy.XPATH, "//android.widget.ImageView[@='...' and @content-desc='扫码']")   
    # 右上角 - 消息图标
    MESSAGE_ICON = (
    AppiumBy.XPATH, "//android.widget.ImageView[@='...' and @content-desc='消息']")   
    # 顶部背景图（可选，用于辅助确认页面）
    TOP_BANNER = (
    AppiumBy.XPATH, "//android.widget.ImageView[@='...' and @content-desc='顶部背景']")   
    # 快速寄件入口 - 容器（包含文字和图标）
    QUICK_SHIPMENT_CONTAINER = (AppiumBy.XPATH,
                                "//android.widget.LinearLayout[@='...' and contains(@content-desc,'快速寄件')]")   
    # 快速寄件入口 - 文字
    QUICK_SHIPMENT_TEXT = (AppiumBy.XPATH, "//android.widget.TextView[@text='快速寄件']")
    # 快速寄件入口 - 描述（1小时上门取件）
    QUICK_SHIPMENT_DESC = (AppiumBy.XPATH, "//android.widget.TextView[@text='1小时上门取件']")
    # 扫一扫入口 - 容器（包含文字和图标）
    SCAN_CONTAINER = (AppiumBy.XPATH,
                      "//android.widget.LinearLayout[@='...' and contains(@content-desc,'扫一扫')]")   
    # 扫一扫入口 - 文字
    SCAN_TEXT = (AppiumBy.XPATH, "//android.widget.TextView[@text='扫一扫']")
    # 扫一扫入口 - 描述（二维码下单查件）
    SCAN_DESC = (AppiumBy.XPATH, "//android.widget.TextView[@text='二维码下单查件']")

    #常用功能
    # 常用功能入口定位器
    ENTRY_NAMES = ["运费时效","总裁信箱","客户满意度","支付开票","实名认证","偏好设置","在线理赔","地址簿"]

    DAILY_FUNTION_ENTRY_LOCATORS = {
        key: (AppiumBy.XPATH,
              f'//android.widget.TextView[@resource-id="com.oxbix.spanlogistics:id/tv_icon" and @text="{key}"]') for key in
        ENTRY_NAMES
    }


    # 个人介绍入口 - 容器（包含文字和箭头）
    PERSONAL_INTRO_CONTAINER = (AppiumBy.XPATH,
                                "//android.widget.LinearLayout[@='...' and contains(@content-desc,'个人介绍')]")   
    # 个人介绍入口 - 文字
    PERSONAL_INTRO_TEXT = (AppiumBy.XPATH, "//android.widget.TextView[@text='个人介绍']")
    # 个人介绍入口 - 描述（介绍客户拿奖金）
    PERSONAL_INTRO_DESC = (AppiumBy.XPATH, "//android.widget.TextView[@text='介绍客户拿奖金']")
    # 预存运费入口 - 容器（包含文字和箭头）
    PREPAY_FREIGHT_CONTAINER = (AppiumBy.XPATH,
                                "//android.widget.LinearLayout[@='...' and contains(@content-desc,'预存运费')]")   
    # 预存运费入口 - 文字
    PREPAY_FREIGHT_TEXT = (AppiumBy.XPATH, "//android.widget.TextView[@text='预存运费']")
    # 预存运费入口 - 描述（限时特惠）
    PREPAY_FREIGHT_DESC = (AppiumBy.XPATH, "//android.widget.TextView[@text='限时特惠']")
    # 客户服务悬浮按钮（带问号图标）
    CUSTOMER_SERVICE_FLOAT_BTN = (
    AppiumBy.XPATH, "//android.widget.ImageView[@='...' and @content-desc='客户服务']")   

    
    # 底部导航栏元素
    TAB_HOME = (AppiumBy.XPATH, '//android.widget.TextView[@resource-id="com.oxbix.spanlogistics:id/tv_tab_title" and @text="首页"')
    TAB_SEND = (AppiumBy.XPATH, '//android.widget.TextView[@resource-id="com.oxbix.spanlogistics:id/tv_tab_title" and @text="寄件"]')
    TAB_QUERY = (AppiumBy.XPATH, '//android.widget.TextView[@resource-id="com.oxbix.spanlogistics:id/tv_tab_title" and @text="查件"]')
    TAB_MINE = (AppiumBy.XPATH, '//android.widget.TextView[@resource-id="com.oxbix.spanlogistics:id/tv_tab_title" and @text="我的"]')



    def click_tab(self, tab_name: str):
        """点击底部导航栏"""
        tab_map = {
            "首页": self.TAB_HOME,
            "寄件": self.TAB_SEND,
            "查件": self.TAB_QUERY,
            "我的": self.TAB_MINE
        }
        self.click(tab_map[tab_name])

    def enter_single_order_page(self):
        """进入快速寄件页"""
        self.click(self.QUICK_SHIPMENT_DESC)


    def enter_scan_page(self):
        """进入扫一扫页面"""
        self.click(self.SCAN_DESC)

    def enter_daily_function_page(self,page_name):
        """进入常用功能页"""
        self.click(self.DAILY_FUNTION_ENTRY_LOCATORS[page_name])

    def enter_prepay_freight_page(self,page_name):
        """进入预存运费页"""
        self.click(self.PREPAY_FREIGHT_CONTAINER)

    def enter_customer_service_page(self,page_name):
        """进入客服服务页"""
        self.click(self.CUSTOMER_SERVICE_FLOAT_BTN)
